import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { Message } from 'element-ui'
import Login from '../views/login/index.vue'
import Layout from '../views/layout/index.vue'
import Dashboard from '../views/dashboard/index.vue'
import Institutions from '@/views/base/institutions'
import Organization from '@/views/base/organization'
import Freight from '@/views/base/freight'
import CarModels from '@/views/cars/car-models'
import Vehicle from '@/views/cars/vehicle'
import Register from '@/views/cars/register'
import Detail from '@/views/cars/components/vehicle-detail'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        { path: '/dashboard', component: Dashboard, meta: '工作台' },
        { path: '/organization', component: Organization, meta: '机构管理' },
        { path: '/institutions', component: Institutions, meta: '机构作业范围' },
        { path: '/freight', component: Freight, meta: '运费管理' },
        { path: '/car-models', component: CarModels, meta: '车型管理' },
        { path: '/vehicle', component: Vehicle, meta: '车辆列表' },
        { path: '/register', component: Register, meta: '回车登记' },
        { path: '/transit/vehicle-detail/:id', component: Detail, meta: '车辆详情 ' },
        { path: '/courier', component: Register, meta: '快递员管理' },
        { path: '/driver', component: Register, meta: '司机管理' },
        { path: '/roster', component: Register, meta: '排班管理' }
      ]
    }
  ]
})

// # 路由全局前置守卫拦截
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path !== '/login' && !token) {
    Message.error('登录过期，请重新登录')
    return next('/login')
  }
  next()
})

export default router
